(function (G) {

    G.component({
        options: {
            multipleSlots: true
        },
        props: {
            type: {
                type: Number,
                default: 1
            }
        },
        data: function () {
            return {
                rows: [],
                total:0,
                selectId:'',
                dialogVisible: false,
                tab: {},
                query:{
                    latitude:0,
                    longitude:0,
                    address:'',
                    pageNum:0,
                    keyword:''
                },
                types:{}
            }
        },
        mounted: function () {
            
        },
        methods: {
            close(){
                this.setData({
                    dialogVisible:false
                })
            },
            searchName(event) {
                this.data.query.keyword = event.detail.value
                this.data.query.pageNum = 0
                this.getList()
            },
            selectValue(event) {
                let index = event.currentTarget.dataset.index
                this.setData({
                    selectId:this.data.rows[index].id
                })
            },
            edit(event) {
                let index = event.currentTarget.dataset.index,tabType = this.data.tab.value
                var modeleditquery = {
                    values: index == -1 ? {} : this.data.rows[index],
                    title: index == -1 ? '新增资源设施' : '修改资源设施',
                    meth: index == -1 ? 'POST' : 'PUT',
                    refresh: (next,data)=>{//后修改
                        if(tabType != 'grid' && data.gridId){//修改资源网格
                            G.get('/api/model/grid',{id:data.gridId}).then(grid => {
                                G.put('/api/model/facility',{
                                    id:data.id,
                                    unitId:grid.unitId,
                                    userId:grid.userId,
                                    enterpriseId:grid.enterpriseId
                                })
                            })
                        }
                    },
                    models: {},
                    url: '/api/model/facility',
                    submit: function (data) {
                        if(tabType != 'grid' && data.address){
                            let address = G.string(data.address).parse({})
                            data.address = address.address
                            data.longitude = address.longitude
                            data.latitude = address.latitude
                        }
                        return data;
                    }
                }
                if(tabType == 'grid'){//修改网格起始点
                    modeleditquery.url = '/api/model/grid'
                    modeleditquery.models = {
                        name:{
                            label:'网格名称',
                            readOnly:true,
                            type:'input'
                        },
                        startDesc:{
                            label:'起点',
                            attr:{
                                placeholder:'请输入起点'
                            },
                            type:'input'
                        },
                        endDesc:{
                            label:'终点',
                            attr:{
                                placeholder:'请输入终点'
                            },
                            type:'input'
                        }
                    }
                }else{//修改资源设施
                    if(modeleditquery.values.address){
                        modeleditquery.values.address = {
                            longitude: modeleditquery.values.longitude,
                            latitude: modeleditquery.values.latitude,
                            address: modeleditquery.values.address
                        }
                    }else{
                        modeleditquery.values.address = {
                            longitude: this.data.query.longitude,
                            latitude: this.data.query.latitude,
                            address: this.data.query.address
                        }
                    }
                    modeleditquery.models = {
                        name:{
                            label:'设备名称',
                            rule: {
                                NotNull: true///不能为空
                            },
                            type:'input'
                        },
                        type:{
                            label:'设施类型',
                            rule:{ NotNull:true },
                            attr:{
                                actions:[
                                    {value:'hw_facility_post',name:'劳动者港湾'},
                                    {value:'hw_facility_transit',name:'垃圾中转站'},
                                    {value:'hw_facility_collect',name:'垃圾收集点'},
                                    {value:'hw_facility_water_pile',name:'水车加水柱'},
                                    {value:'hw_facility_advertising',name:'户外广告'},
                                    {value:'hw_facility_outfit',name:'垃圾分类设施'}
                                ]
                            },
                            type:'select'
                        },
                        gridId:{
                            label:'所属网格',
                            rule:{ NotNull:true },
                            attr:{
                                skey: 'name',///显示字段，默认name
                                vkey: 'id',///取值字段，默认id
                                url: '/api/model/grid?status=1',///查询API
                                canClear: true,///是否可清除
                                title: '请选择所属网格'///标题
                            },
                            type:'onlyselect'
                        },
                        address: {
                            label: '地点',
                            rule:{ NotNull:true },
                            attr: {
                                type: 'mapaddress'
                            },
                            type: 'customer'
                        }
                    }
                    if(tabType == 'hw_facility_toilet'){
                        modeleditquery.models.type.attr.actions = [
                            {value:'hw_facility_toilet',name:'公厕'}
                        ]
                        modeleditquery.models.gridId.attr.url += '&resType=hw_facility_toilet' 
                        //零时数据采集的时候需要 新增的公厕属于石桥铺环卫所
                        // modeleditquery.models.gridId = null
                        // if(index == -1) modeleditquery.models.gridId = '1641341364933365760'
                    }else if(tabType == 'hw_facility_septic_tank'){
                        modeleditquery.models.gridId.attr.url += '&resType=hw_facility_septic_tank' 
                        modeleditquery.models.type.attr.actions = [
                            {value:'hw_facility_septic_tank',name:'化粪池'}
                        ]
                    }else{
                        modeleditquery.models.gridId.attr.url += '&resType=other' 
                    }
                }
                G.Storage.set('modeleditquery', modeleditquery)
                G.$go('/pages/model/edit/edit')
            },
            getGrid(args){
                args = args || {}
                return G.get(args.type == 'grid' ? '/v1/map/getGrid' : '/v1/facility', args)
            },
            getDefault(args){
                args.type = args.tab.value
                this.getGrid(args).then(res => {
                    this.setData({
                        total:res.total,
                        rows:res.rows,
                        selectId:res.rows[0].id
                    })
                    this.submit(1)
                })
            },
            open(args) {
                args.type = args.tab.value
                this.setData({
                    selectId:args.selectId,
                    tab:args.tab,
                    dialogVisible: true,
                    query:{
                        latitude:args.latitude,
                        longitude:args.longitude,
                        address:args.address,
                        pageNum:0,
                        type:args.tab.value,
                        // keyword:''
                    }
                })
                this.getList()
            },
            getList(){
                let query = this.data.query
                query.pageNum += 1
                this.getGrid(query).then(res => {
                    this.setData({
                        query,
                        rows:query.pageNum == 1 ? res.rows : this.data.rows.concat(res.rows),
                        total:res.total
                    })
                })
            },
            close(event) {
                this.triggerEvent('change', {
                    event
                })
                this.setData({
                    dialogVisible: false
                })
            },
            submit(event) {
                let index = -1
                for(let i = 0; i < this.data.rows.length; i ++){
                    if(this.data.rows[i].id == this.data.selectId){
                        index = i
                        break
                    }
                }
                if(index > -1){
                    let unit = this.data.rows[index]
                    let types = [
                        {value:'hw_facility_toilet',name:'公厕'},
                        {value:'hw_facility_post',name:'劳动者港湾'},
                        {value:'hw_facility_transit',name:'垃圾中转站'},
                        {value:'hw_facility_collect',name:'垃圾收集点'},
                        {value:'hw_facility_water_pile',name:'水车加水柱'},
                        {value:'hw_facility_advertising',name:'户外广告'},
                        {value:'hw_facility_septic_tank',name:'化粪池'},
                        {value:'hw_facility_outfit',name:'垃圾分类设施'}
                    ]
                    for(let i = 0; i < types.length; i ++) {
                        if(types[i].value == unit.type) {
                            unit.typeName = types[i].name
                            break;
                        }
                    }
                    let userIds = [unit.userId]
                    if(unit.gridId){
                        unit.inventoryId = unit.id
                        unit.id = unit.gridId
                    }
                    if(unit.userId1){//协助处理人
                        unit.userId1 = G.string(unit.userId1).parse([])
                        for(let i = 0; i < unit.userId1.length; i ++){
                            userIds.push(unit.userId1[i].id)
                        }
                    }
                    this.triggerEvent('change', {
                        units: [unit],
                        unitIndex: 0,
                        userId:userIds.join(','),
                        event
                    })
                    this.setData({
                        dialogVisible: false
                    })
                }else{
                    G.toask('请选择')
                }
            }
        }
    })
})(wx.$)